package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.model.Email;
import com.stylefeng.guns.modular.system.model.EmailUser;
import com.stylefeng.guns.modular.system.service.IEmailService;
import com.stylefeng.guns.modular.system.service.IEmailUserService;
import com.stylefeng.guns.modular.system.warpper.EmailWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 邮件地址管理控制器
 *
 * @author fengshuonan
 * @Date 2018-05-18 23:16:48
 */
@Controller
@RequestMapping("/email")
public class EmailController extends BaseController {

    private String PREFIX = "/system/email/";

    @Autowired
    private IEmailService emailService;
    @Autowired
    private IEmailUserService emailUserService;

    /**
     * 跳转到邮件地址管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "email.html";
    }

    /**
     * 跳转到添加邮件地址管理
     */
    @RequestMapping("/email_add")
    public String emailAdd() {
        return PREFIX + "email_add.html";
    }

    /**
     * 跳转到修改邮件地址管理
     */
    @RequestMapping("/email_update/{emailId}")
    public String emailUpdate(@PathVariable Integer emailId, Model model) {
        Email email = emailService.selectById(emailId);
        model.addAttribute("item", email);
        LogObjectHolder.me().set(email);
        return PREFIX + "email_edit.html";
    }

    /**
     * 获取邮件地址管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String address, @RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime) {
        List<Map<String, Object>> emails = emailService.selectEmailLikeAddress(address, beginTime, endTime);

        return new EmailWarpper(emails).warp();
    }

    /**
     * 新增邮件地址管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Email email) {
        // 判断地址是否重复
        Email email1 = emailService.selectByAddress(email.getAddress());
        if (email1 != null) {
            throw new GunsException(BizExceptionEnum.EMAIL_ADDRESS_EXISTED);
        }

        //完善创建时间
        email.setCreatetime(new Date());
        emailService.insert(email);
        // 获取刚插入的记录的id
        int emailId = emailService.selectLastInsertId();
        //同步插入email和user的关系表
        EmailUser emailUser = new EmailUser();
        emailUser.setCreatetime(new Date());
        emailUser.setEmailId(emailId);
        // 获取当前登陆用户的id
        int userId = ShiroKit.getUser().getId();
        emailUser.setUserId(userId);
        emailUser.setStatus(1);
        emailUserService.insert(emailUser);
        return SUCCESS_TIP;
    }

    /**
     * 删除邮件地址管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer emailId) {
        emailService.deleteById(emailId);
        // 查询出 邮件用户关系表该记录的ID,并同步删除记录
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", ShiroKit.getUser().getId());
        map.put("email_id", emailId);
        List<EmailUser> list = emailUserService.selectByMap(map);
        EmailUser eu = list.get(0);
        emailUserService.deleteById(eu.getId());
        return SUCCESS_TIP;
    }

    /**
     * 修改邮件地址管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Email email) {
        emailService.updateById(email);
        return SUCCESS_TIP;
    }

    /**
     * 邮件地址管理详情
     */
    @RequestMapping(value = "/detail/{emailId}")
    @ResponseBody
    public Object detail(@PathVariable("emailId") Integer emailId) {
        return emailService.selectById(emailId);
    }
}
